package com.fcai.microserv.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 服务响应码模型，表示一个服务的调用结果
 *
 * @author wangbin
 * @since 1.0.0
 */
@ApiModel(value="服务响应码", description="描述API接口的返回结果的状态")
@Getter
@Setter
@NoArgsConstructor
public final class ServiceRespModel implements Serializable {

    @ApiModelProperty("响应代码")
    private String code;

    @ApiModelProperty("代码描述")
    private String message;

    /**
     * @param code    响应代码
     * @param message 代码表示的信息
     */
    public ServiceRespModel(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 当前对象是否为期望的code，如果不传code,则默认为CommonRespCode.OK
     *
     * @param expected 期望的返回码
     * @return true->与期望相符; False->不相符
     * @since 1.0.0
     */
    public boolean isExpected(ServiceRespModel expected) {
        if (expected != null && this.code.equals(expected.getCode())) {
            return true;
        }

        if (expected != null && (StringUtils.equalsIgnoreCase(this.code, CommonRespCode.SUCCESS.code))) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return String.format("%s-%s", this.code, this.message);
    }

}
